//
//  WhTSprite.m
//  ProjectM
//
//  Created by ris on 11-3-23.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "WhTSprite.h"
#import "Texture2D.h"

// 旋转矩阵
GLfloat rm_x[]=
{1,0,0,0,
	0,cos(M_PI/15),sin(M_PI/15),0,
	0,-sin(M_PI/15),cos(M_PI/15),0,
	0,0,0,1};

GLfloat rm_xn[]=
{1,0,0,0,
	0,cos(M_PI/15),-sin(M_PI/15),0,
	0,sin(M_PI/15),cos(M_PI/15),0,
	0,0,0,1};

GLfloat rm_y[]=
{cos(M_PI/15),0,-sin(M_PI/15),0,
	0,1,0,0,
	sin(M_PI/15),0,cos(M_PI/15),0,
	0,0,0,1};

GLfloat rm_yn[]=
{cos(M_PI/15),0,sin(M_PI/15),0,
	0,1,0,0,
	-sin(M_PI/15),0,cos(M_PI/15),0,
	0,0,0,1};

GLfloat rm_z[]=
{cos(M_PI/15),sin(M_PI/15),0,0,
	-sin(M_PI/15),cos(M_PI/15),0,0,
	0,0,1,0,
	0,0,0,1};

GLfloat rm_zn[]=
{cos(M_PI/15),-sin(M_PI/15),0,0,
	sin(M_PI/15),cos(M_PI/15),0,0,
	0,0,1,0,
	0,0,0,1};

GLfloat rm_n[]=
{1,0,0,0,
	0,1,0,0,
	0,0,1,0,
	0,0,0,1};

GLfloat *rm= rm_y;


//GLfloat matrix_[96] = {-1.0f,1.0f,1.0f,1.0f,
//	-1.0f,-1.0f,1.0f,1.0f,
//	1.0f,-1.0f,1.0f,1.0f,
//	1.0f,1.0f,1.0f,1.0f, // 前表面
//	-1.0f,1.0f,1.0f,1.0f,
//	-1.0f,1.0f,-1.0f,1.0f,
//	-1.0f,-1.0f,-1.0f,1.0f,
//	-1.0f,-1.0f,1.0f,1.0f, // 左侧面
//	1.0f,1.0f,1.0f,1.0f,
//	1.0f,1.0f,-1.0f,1.0f,
//	1.0f,-1.0f,-1.0f,1.0f,
//	1.0f,-1.0f,1.0f,1.0f, // 右侧面
//	1.0f,1.0f,1.0f,1.0f,
//	1.0f,1.0f,-1.0f,1.0f,
//	-1.0f,1.0f,-1.0f,1.0f,
//	-1.0f,1.0f,1.0f,1.0f, // 顶部
//	1.0f,-1.0f,-1.0f,1.0f,
//	-1.0f,-1.0f,-1.0f,1.0f,
//	-1.0f,-1.0f,1.0f,1.0f,
//	1.0f,-1.0f,1.0f,1.0f, // 底部
//	-1.0f,1.0f,-0.2f,1.0f,
//	-1.0f,-1.0f,-0.2f,1.0f,
//	1.0f,-1.0f,-0.2f,1.0f,
//	1.0f,1.0f,-0.2f,1.0f // 背面
//};

GLfloat m[96] = {-1.0f,1.0f,1.0f,1.0f,
	-1.0f,-1.0f,1.0f,1.0f,
	1.0f,-1.0f,1.0f,1.0f,
	1.0f,1.0f,1.0f,1.0f, // 前表面
	-1.0f,1.0f,1.0f,1.0f,
	-1.0f,1.0f,0.8f,1.0f,
	-1.0f,-1.0f,0.8f,1.0f,
	-1.0f,-1.0f,1.0f,1.0f, // 左侧面
	1.0f,1.0f,1.0f,1.0f,
	1.0f,1.0f,0.8f,1.0f,
	1.0f,-1.0f,0.8f,1.0f,
	1.0f,-1.0f,1.0f,1.0f, // 右侧面
	1.0f,1.0f,1.0f,1.0f,
	1.0f,1.0f,0.8f,1.0f,
	-1.0f,1.0f,0.8f,1.0f,
	-1.0f,1.0f,1.0f,1.0f, // 顶部
	1.0f,-1.0f,0.8f,1.0f,
	-1.0f,-1.0f,0.8f,1.0f,
	-1.0f,-1.0f,1.0f,1.0f,
	1.0f,-1.0f,1.0f,1.0f, // 底部
	-1.0f,1.0f,0.8f,1.0f,
	-1.0f,-1.0f,0.8f,1.0f,
	1.0f,-1.0f,0.8f,1.0f,
	1.0f,1.0f,0.8f,1.0f // 背面
};

GLfloat m_n[96];

const short texture_coords[] = {
	// Front face
	//	0, 0,       // top left
	//		0, 1,       // bottom left
	//		1, 1,       // bottom right
	//		1, 0,       // top right
	0,1,
	1,1,
	1,0,
	0,0,
	// Top face
	0, 1,       // top left
	0, 0,       // bottom left
	1, 0,       // bottom right
	1, 1,       // top right
	
	// Rear face
	0, 1,       // top left
	0, 0,       // bottom left
	1, 0,       // bottom right
	1, 1,       // top right
	
	// Bottom face
	0, 1,       // top left
	0, 0,       // bottom left
	1, 0,       // bottom right
	1, 1,       // top right
	
	// Left face
	0, 1,       // top left
	0, 0,       // bottom left
	1, 0,       // bottom right
	1, 1,       // top right
	
	// Right face
	0, 1,       // top left
	0, 0,       // bottom left
	1, 0,       // bottom right
	1, 1      // top right
};



@interface WhTSprite ()
- (void)loadTexture;
@end


@implementation WhTSprite
@synthesize position = position_;
@synthesize scale = scale_;


- (id)init
{
	if (self=[super init])
	{
		
	}
	return self;
}

- (id)initWithPaths:(id)firstObject, ...
{
	if (self=[super init]) 	
	{
		res_ = [[NSMutableArray alloc] initWithObjects:@"strNil",@"strNil",
						@"strNil",@"strNil",
						@"strNil",@"strNil",nil];
		
		position_ = WhTPointZero;
		scale_ = WhTPointMake(1.0, 1.0, 1.0);
		
		id eachObject;
		va_list argumentList;
		if (firstObject) 
		{                
			[res_ replaceObjectAtIndex:0 withObject:firstObject];
			va_start(argumentList, firstObject);
			short i = 0;
			while (eachObject = va_arg(argumentList, id))
				[res_ replaceObjectAtIndex:++i withObject:eachObject]; 
			va_end(argumentList);
		}
		[self loadTexture];
	}
	return self;
}

- (void)render
{
	
	
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	
		glBindTexture(GL_TEXTURE_2D, texture_id[0]);
	glEnable(GL_TEXTURE_2D);
	
	
	glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_BLEND);
		
		glTexCoordPointer(2, GL_SHORT, 0, texture_coords);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);


	glPushMatrix();
	{	
		glTranslatef(position_.x, position_.y, position_.z);
	//	glScalef(scale_.x, scale_.y, scale_.z);
		glVertexPointer(4, GL_FLOAT, 0, m);
		glEnableClientState(GL_VERTEX_ARRAY);
		
		//front face
		//if (glIsTexture(texture_id[0]))
//		{
//			glEnableClientState(GL_TEXTURE_COORD_ARRAY);
//			glBindTexture(GL_TEXTURE_2D, texture_id[0]);
//		}
//		else
//		{
//			glDisableClientState(GL_TEXTURE_COORD_ARRAY);
//		}
		
		glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
		//glDisable(GL_BLEND);
		//left face
		//if (glIsTexture(texture_id[1]))
//		{
//			glEnableClientState(GL_TEXTURE_COORD_ARRAY);
//			glBindTexture(GL_TEXTURE_2D, texture_id[1]);
//		}
//		else
//		{
//			glDisableClientState(GL_TEXTURE_COORD_ARRAY);
//		}
		glDrawArrays(GL_TRIANGLE_FAN, 4, 4);
		
		//right face
		//if (glIsTexture(texture_id[2]))
//		{
//			glEnableClientState(GL_TEXTURE_COORD_ARRAY);
//			glBindTexture(GL_TEXTURE_2D, texture_id[2]);
//		}
//		else
//		{
//			glDisableClientState(GL_TEXTURE_COORD_ARRAY);
//		}
		glDrawArrays(GL_TRIANGLE_FAN, 8, 4);
		
		//up face
		//if (glIsTexture(texture_id[3]))
//		{
//			glEnableClientState(GL_TEXTURE_COORD_ARRAY);
//			glBindTexture(GL_TEXTURE_2D, texture_id[3]);
//		}
//		else
//		{
//			glDisableClientState(GL_TEXTURE_COORD_ARRAY);
//		}
		glDrawArrays(GL_TRIANGLE_FAN, 12, 4);
		
		//bottom face
		//if (glIsTexture(texture_id[4]))
//		{
//			glEnableClientState(GL_TEXTURE_COORD_ARRAY);
//			glBindTexture(GL_TEXTURE_2D, texture_id[4]);
//		}
//		else
//		{
//			glDisableClientState(GL_TEXTURE_COORD_ARRAY);
//		}
		glDrawArrays(GL_TRIANGLE_FAN, 16, 4);
		
		//near face
		//if (glIsTexture(texture_id[5]))
//		{
//			glEnableClientState(GL_TEXTURE_COORD_ARRAY);
//			glBindTexture(GL_TEXTURE_2D, texture_id[5]);
//		}
//		else
//		{
//			glDisableClientState(GL_TEXTURE_COORD_ARRAY);
		
//		}
	//		glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	 //glBindTexture(GL_TEXTURE_2D, texture_id[1]);
		glDrawArrays(GL_TRIANGLE_FAN, 20, 4);
	}
	glPopMatrix();
	
	//glEnable(GL_DEPTH_TEST); 
	//[self rotateByType:WhTRotateTypeX];
	
}

- (void)loadTexture
{
	glGenTextures(6, &texture_id[0]);
	for (int i = 0;i<[res_ count];++i)
	{
		if (![[res_ objectAtIndex:i] isEqualToString:@"strNil"])
		{
			Texture2D *tex = [[Texture2D alloc] initWithImage:
												[UIImage imageNamed:[res_ objectAtIndex:i]]];
			texture_id[i] = [tex name];

			printf("-----------------------------------texture id%d\n",texture_id[i]);

		}
	}
}


- (void)dealloc
{
	glDeleteTextures(6, &texture_id[0]);
	[res_ release];
	[super dealloc];
}

- (void)rotateByType:(WhTRotateType)rotateType
{
	switch (rotateType) {
		case WhTRotateTypeX:rm = rm_x;break;
		case WhTRotateTypeY:rm = rm_y;break;
		case WhTRotateTypeZ:rm = rm_z;break;
		case WhTRotateTypeAny:break;
		case WhTRotateTypeNone:rm = rm_n;break;
		default:break;
	}
	
	for(int i=0;i<24;i++)
		for (int j=0;j<4;j++)
			m_n[i*4+j] = m[i*4+0]*rm[j*4+0]
									 +m[i*4+1]*rm[j*4+1]
									 +m[i*4+2]*rm[j*4+2]
								   +m[i*4+3]*rm[j*4+3];
	
	for (int i=0;i<96;i++)
		m[i] = m_n[i];
}

@end
